from SAES import encipher, decipher,binary_list_to_string,Bit32process,Bit32ProcessTriple,TotalCrackDown
import sys  
from PySide6.QtWidgets import QApplication, QMainWindow  
from SAESGUI_ui import Ui_MainWindow  # 导入生成的 UI 类  
from PySide6.QtCore import Slot
import numpy as np

class MainWindow(QMainWindow, Ui_MainWindow):  # 定义主窗口类  
    def __init__(self):  
        super().__init__()  
        self.setupUi(self)  # 初始化 UI  

        # 这里可以添加信号与槽、其他逻辑等  
        self.jiaMi.clicked.connect(self.mencipher) 
        self.jieMi.clicked.connect(self.mdecipher)
        self.crack.clicked.connect(self.forceCrack)
        self.doubleEncipher.clicked.connect(self.dEncipher)
        self.tEncipher.clicked.connect(self.triEncipher)

    @Slot()
    def mencipher(self):#加密
        s=self.inputText.text()
        k=self.keyText.text()
        flag=0
        if len(s)==2:#检测是否是2字节字符串
            s=s.encode('ascii')
            s=''.join(format(bit,'08b') for bit in s)
            flag=1
        if len(k)==2:
            k=k.encode('ascii')
            k=''.join(format(bit,'08b') for bit in k)
        #防止明文或密钥以0开头导致错误
        if isinstance(s,int):
            s=str(bin(s)[2:]).zfill(16)
        if isinstance(k,int):
            k=str(bin(k)[2:]).zfill(16)
        #转为一维二进制数组
        s=np.array([int(bit) for bit in s])
        k=np.array([int(bit) for bit in k])
        temp=encipher(s,k)
        if flag==1:
            temp=binary_list_to_string(temp)
            print('1')
        else:
            temp=''.join(str(bit) for bit in temp)
        print(temp)
        self.outputText.setText(temp)
    def mdecipher(self):#解密
        s=self.outputText.text()
        k=self.keyText.text()
        flag=0
        if len(s)==2:#检测是否是2字节字符串
            s=s.encode('ascii')
            s=''.join(format(bit,'08b') for bit in s)
            flag=1
        if len(k)==2:
            k=k.encode('ascii')
            k=''.join(format(bit,'08b') for bit in k)
        #防止密文或密钥以0开头导致错误
        if isinstance(s,int):
            s=str(bin(s)[2:]).zfill(16)
        if isinstance(k,int):
            k=str(bin(k)[2:]).zfill(16)
        #转为一维二进制数组
        s=np.array([int(bit) for bit in s])
        k=np.array([int(bit) for bit in k])
        temp=decipher(s,k)
        if flag==1:
            temp=binary_list_to_string(temp)
        else:
            temp=''.join(str(bit) for bit in temp)
        print(temp)
        self.inputText.setText(temp)

    def dEncipher(self):#双重加密
        s=self.inputText.text()
        k=self.keyText.text()
        flag=0
        mode=''
        if len(s)==2:#检测是否是2字节字符串
            # s=s.encode('ascii')
            # s=''.join(format(bit,'08b') for bit in s)
            flag=1
            mode='ASCIImode'
        if len(k)==4:
            k=k.encode('ascii')
            k=''.join(format(bit,'08b') for bit in k)
        #转为一维二进制数组
        # s=np.array([int(bit) for bit in s])
        k=np.array([int(bit) for bit in k])
        temp=Bit32process('cipher',mode,s,k)
        if mode!='ASCIImode':
            temp=''.join(str(bit) for bit in temp)
        self.outputText.setText(temp)

    def triEncipher(self):#三重加密
        s=self.inputText.text()
        k=self.keyText.text()
        mode=''
        if len(s)==2:#检测是否是2字节字符串
            mode='ASCIImode'
        else:
            s=np.array([int(bit) for bit in s])
        if len(k)==4:
            k=k.encode('ascii')
            k=''.join(format(bit,'08b') for bit in k)
        #转为一维二进制数组
        
        k=np.array([int(bit) for bit in k])
        temp=Bit32ProcessTriple('cipher',mode,s,k)
        if mode!='ASCIImode':
            temp=''.join(str(bit) for bit in temp)
        self.outputText.setText(temp)

    def forceCrack(self):#破解
        s1=self.inputText.text()
        s2=self.outputText.text()
        mode=''
        if len(s1)==2:#检测是否是2字节字符串
            mode='ASCIImode'
        #防止明文或密文以0开头导致错误
        if isinstance(s1,int):
            s1=str(bin(s1)[2:]).zfill(8)
        if isinstance(s2,int):
            s2=str(bin(s2)[2:]).zfill(8)
        
        if mode=='ASCIImode':
            key=TotalCrackDown(mode,s1,s2)
        else:
            s1=np.array([int(bit) for bit in s1])
            s2=np.array([int(bit) for bit in s2])
            key=TotalCrackDown('1',s1,s2)
        keyOut=''
        for k in key:
            keyOut+=(str(k)+',')
        self.keyText.setText(keyOut)

if __name__ == "__main__":  
    app = QApplication(sys.argv)  # 创建应用程序实例  
    window = MainWindow()  # 创建主窗口实例  
    window.show()  # 显示窗口  
    sys.exit(app.exec_())  # 运行应用程序